import * as types from '../actions/mainScreenActionTypes';
import testinitDate from './TestinitDate';//这个是默认的测试数据
import TimerMixin from 'react-timer-mixin';
//库
import _ from 'underscore';

const initialState=testinitDate;

export default function mainscreenRedux(state = initialState, action = {}) {
  switch (action.type) {
    //===================================选择猪场==========================================
    case types.SELECTFARM:
      return {
        ...state,
        mainscreen:{
          ...state.mainscreen,
          title:state.mainscreen.farmlistDetail[action.index]
        }
      };
    //============================listview滚动到特定的section时改变===========================
    case types.CHANGEFARMUNIT:
      return {
        ...state,
        mainscreen:{
          ...state.mainscreen,
          farmunit:action.farmunitname
        }
      };
    //==================================单击bwlist的时候=======================================
    case types.ENTER_FARM_UNIT:
      return {
        ...state,
        bwmain:{
          ...state.bwmain,
          currentUnit:state.mainscreen.title+'/'+action.currentSec,
          currentAddr:action.currentAddr
        }
      };
    // =======================登录页面的地方改变mongodb的ip和port=============================
    case types.CHANG_MONGO_IP_PORT:
      return{
        ...state,
        system:{
          ...state.system,
          mongodbip:action.ip,
          mongodbport:action.port
        }
      };
    //开始fetch
    case types.SET_FETCTH_STATE:
      return{
        ...state,
        system:{
          ...state.system,
          fetchstate:'fetching'//告诉ui正在拉取
        }
      }
    // ======================获取首屏渲染用的数据(fetch成功后)======================
    case types.UPDATA_MAINSCREEN:
      return{
        ...state,
        system:{
          ...state.system,
          fetchstate:'success'//告诉ui正在拉取
        },
        mainscreen:{
          ...state.mainscreen,
          title:_.chain(action.prarStructJson).pluck('Farm').uniq().value()[0],
          farmlistDetail:_.chain(action.prarStructJson).pluck('Farm').uniq().value(),//获取每一个实例的Farm值组成一个数组,再将这个数组去重
          bwlist:{
            一线分娩1:action.prarStructJson
          }
        }
      };
    //==========================拉取日下料记录成功==============================
    case types.UPDATA_DAYRECORDDATA:
      return{
        ...state,
        bwmain:{
          ...state.bwmain,
          dayrecord:_.chain(action.dayRecordJson).map(function(item){
            var newitem=_.pick(item,'Amount','Actual','Date');
            newitem.Amount=Number(newitem.Amount.$numberLong);
            newitem.Actual=Number(newitem.Actual.$numberLong);
            return newitem
          }).value()
        }
      }
    //=======================设置为测试模式============================
    case types.SET_TESTMODE:
      return{
        ...state,
        system:{
          ...state.system,
          ifTestmode:true//告诉ui正在拉取
        }
      }
    //=======================设置为测试模式============================
    case types.DONTSET_TESTMODE:
      return{
        ...state,
        system:{
          ...state.system,
          ifTestmode:false//告诉ui正在拉取
        }
      }
    //==========================如果拉取失败==================================
    case types.FETCH_FAILD:
      return{
        ...state,
        system:{
          ...state.system,
          fetchstate:'error'//告诉ui正在拉取
        }
      }
    default:
      return state;
  }
}
